package com.dcrzl.shcz.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcrzl.shcz.common.entity.sys.SysMenu;
import com.dcrzl.shcz.common.entity.sys.SysRoleMenu;
import com.dcrzl.shcz.common.mapper.sys.SysMenuMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.ResultUtils;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.module.sys.service.SysMenuService;
import com.dcrzl.shcz.module.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public Result<List<SysMenu>> selectList(SysMenu sysMenu) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(Objects.nonNull(sysMenu.getStatus()),SysMenu::getStatus,sysMenu.getStatus());
        return ResultUtils.selectResult(baseMapper.selectList(wrapper));
    }

    @Override
    public PageInfo<SysMenu> selectPage(SysMenu sysMenu) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        return super.selectPage(sysMenu, wrapper);
    }

    /**
     *
     * @param roleId 角色ID
     * @return
     */
    @Override
    public Result getUserMenuList(Long roleId) {
        //检测是最高权限者，则把所有菜单查询出来
        if (Objects.equals(roleId, SysConstant.ROOT_ID)) {
            SysMenu sysMenu = new SysMenu();
            sysMenu.setStatus(SysConstant.DATA_NORMAL_STATUS);
            return this.selectList(sysMenu);
        }
        //根据roleId查询菜单ID
        return this.selectMenuListByRoleId(roleId, SysConstant.DATA_NORMAL_STATUS);
    }


    @Override
    public Result<List<SysMenu>> selectMenuListByRoleId(Long roleId, Integer status) {
        Result<List<SysMenu>> menuResult = new Result<>();
        Result<List<SysRoleMenu>> listResult = sysRoleMenuService.selectList(new SysRoleMenu().setRoleId(roleId).setStatus(status));
        if(listResult.isSuccess()){
            List<SysRoleMenu> sysRoleMenus = listResult.getData();
            Set<Serializable> menuIds = new HashSet<>();
            sysRoleMenus.forEach(sysRoleMenu -> {
                menuIds.add(sysRoleMenu.getMenuId());
            });
            SysMenu sysMenu = new SysMenu().setStatus(SysConstant.DATA_NORMAL_STATUS);
            menuResult = this.selectBatchIdsAndParam(menuIds,sysMenu);
        }else{
            menuResult.setSuccess(false).setMsg("数据为空");
        }
        return menuResult;
    }

    public Result<List<SysMenu>> selectBatchIdsAndParam(Collection<Serializable> menuIds, SysMenu sysMenu) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Objects.nonNull(menuIds),SysMenu::getId,menuIds);
        wrapper.ge(Objects.nonNull(sysMenu.getStatus()),SysMenu::getStatus,sysMenu.getStatus());
        return super.selectList(wrapper);
    }

    @Override
    public Result getSubMenu(List<SysMenu> list) {
        //菜单目录列表
        List<SysMenu> sysMenuList = new ArrayList<>();
        list.forEach(sysMenu -> {
            if (sysMenu != null) {
                if (sysMenu.getType() != null
                        && (Objects.equals(sysMenu.getType(), SysConstant.LIST_MENU_TYPE)
                        || Objects.equals(sysMenu.getType(), SysConstant.MENU_TYPE))) {
                    sysMenuList.add(sysMenu);
                }
            }
        });
        // 将id和菜单绑定
        HashMap<Long, SysMenu> menuMap = new HashMap<>();
        for (SysMenu s : sysMenuList) {
            menuMap.put(s.getId(), s);
            s.setList(new ArrayList<>());
        }
        // 使用迭代器,组装菜单的层级关系
        Iterator<SysMenu> iterator = sysMenuList.iterator();
        while (iterator.hasNext()) {
            SysMenu menu = iterator.next();
            SysMenu parent = menuMap.get(menu.getParentId());
            if (Objects.nonNull(parent)) {
                parent.getList().add(menu);
                // 将这个菜单从当前节点移除
                iterator.remove();
            }
        }
        return new Result<>().setData(sysMenuList);
    }


    @Override
    public Result<Integer> removeById(Serializable id) {
        SysMenu sysMenu = new SysMenu()
                .setId((Long) id)
                .setStatus(SysConstant.DATA_DELETE_STATUS);
        return super.updateById(sysMenu);
    }
}